package com.example.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.blog.domain.Message;
import com.example.blog.domain.OperationalLogs;
import com.example.blog.domain.Response;
import com.example.blog.dto.message.AdminSelectMessageDto;
import com.example.blog.note.LogInfo;
import com.example.blog.note.NoToken;
import com.example.blog.note.Permissions;
import com.example.blog.service.impl.MessageServiceImpl;
import com.example.blog.service.impl.OperationalLogsServiceImpl;
import com.example.blog.utils.JwtUtils;
import com.example.blog.dto.message.MessageInsertDto;
import com.example.blog.dto.publicDto.QuerySearchDto;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@RestController
@Transactional
@Validated
@RequestMapping("/message")
public class MessageController {
    private final MessageServiceImpl messageService;
    private final OperationalLogsServiceImpl operationalLogsService;
    @Autowired
    public MessageController(MessageServiceImpl messageService,OperationalLogsServiceImpl operationalLogsService){
        this.messageService=messageService;
        this.operationalLogsService = operationalLogsService;
    }

    //获取所有留言信息
    @NoToken
    @LogInfo("获取所有留言")
    @GetMapping("/getAll/{index}")
    public Response getAll(@PathVariable("index") int index){
        IPage page = new Page(index,13);
        LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Message::getDateTime);
        return new Response(Response.SUCCEED_CODE,"获取所有留言",messageService.page(page,wrapper).getRecords());
    }

    //新增留言信息
    @NoToken
    @LogInfo("新增留言信息")
    @PostMapping("/insert")
    public Response insert(@RequestBody MessageInsertDto messageVo){
        Message message =new Message(messageVo.getAuto(),messageVo.getEmail(),messageVo.getSystem(),messageVo.getContent());
        return new Response(Response.SUCCEED_CODE,"新增留言信息",messageService.save(message));
    }

    //获取所有留言信息(后台管理)
    @Permissions("/message/getAll/admin")
    @LogInfo("获取所有留言信息(后台管理)")
    @PostMapping("/getAll/admin")
    public Response getAllAdmin(@RequestBody AdminSelectMessageDto messageVo){
        IPage page = new Page(messageVo.getIndex(),messageVo.getPage());
        LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Objects.nonNull(messageVo.getDateTime()),Message::getDateTime,messageVo.getDateTime())
                .like(Objects.nonNull(messageVo.getCondition() ),Message::getName,messageVo.getCondition())
                .or().like(Objects.nonNull(messageVo.getCondition()),Message::getAuto,messageVo.getCondition())
                .orderByDesc(Message::getDateTime);
        return new Response(Response.SUCCEED_CODE,"获取所有留言信息(后台管理)",messageService.page(page,wrapper));
    }

    //删除留言信息(后台管理)
    @Permissions("/message/delete/admin")
    @LogInfo("删除留言信息(后台管理)")
    @PostMapping("/delete/admin")
    public Response deleteAdmin(@Validated @RequestBody QuerySearchDto messageVo){
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        String  token = request.getHeader("token");
        Claims claims =  JwtUtils.checkToken(token);
        boolean result = messageService.removeById(messageVo.getIndex());
        OperationalLogs operationalLogs = new OperationalLogs();
        operationalLogs.setType(1);
        operationalLogs.setOutcome(1);
        operationalLogs.setUser("留言："+messageVo.getCondition());
        operationalLogs.setAdmin((String) claims.get("username"));
        operationalLogs.setContent("删除留言信息");
        operationalLogsService.save(operationalLogs);
        return new Response(Response.SUCCEED_CODE,"删除留言信息(后台管理)",result);
    }
}
